package query

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.serializer.SerializerFeature
import com.alipay.api.domain.{AlipayTradeFastpayRefundQueryModel, AlipayTradeQueryModel}
import com.alipay.api.request.{AlipayTradeFastpayRefundQueryRequest, AlipayTradeQueryRequest}
import dto.req.QueryOrderReqDTO
import dto.resp.QueryOrderRespDTO
import enums.{PayChannelEnum, PayEnum, ThirdSystemErrorEnum}
import factory.ActionFactory
import org.springframework.beans.factory.InitializingBean
import org.springframework.stereotype.Service

@Service
class AliPayQueryAction extends BaseQueryAction with InitializingBean{

  override def getPayChannel: Integer = PayChannelEnum.ALI_PAY.getId

  @throws[Exception]
  override def query[T <: QueryOrderReqDTO, R <: QueryOrderRespDTO](req: T, resp: R): R = {
    val queryRequest = new AlipayTradeQueryRequest
    val tradeQueryModel = new AlipayTradeQueryModel
    tradeQueryModel.setOutTradeNo(req.getPayOrderNo)
    queryRequest.setBizModel(tradeQueryModel)
    log.info("alipay query request params : {}", JSON.toJSONString(queryRequest, SerializerFeature.PrettyFormat))
    val queryResponse = alipayClient.execute(queryRequest)
    log.debug("alipay query response result : {}", JSON.toJSONString(queryResponse, SerializerFeature.PrettyFormat))
    //BeanUtils.copyProperties(req, resp);
    if (!queryResponse.isSuccess) {
      resp.setOrderStatusCode(PayEnum.PayTradeStatusEnum.UN_KNOW.getCode)
      resp.setOrderStatusMessage(PayEnum.PayTradeStatusEnum.UN_KNOW.getDesc)
      resp.setThirdOrderStatusCode(ThirdSystemErrorEnum.getErrorEnum(queryResponse.getSubCode).getMessage)
      resp.setThirdOrderStatusMessage(ThirdSystemErrorEnum.getErrorEnum(queryResponse.getSubCode).getCode)
    }
    else {
      resp.setThirdOrderNo(queryResponse.getTradeNo)
      resp.setThirdOrderStatusCode(PayEnum.PayTradeStatusEnum.getByThirdCode(queryResponse.getTradeStatus, req.getPayChannel).getThirdCode)
      resp.setThirdOrderStatusMessage(PayEnum.PayTradeStatusEnum.getByThirdCode(queryResponse.getTradeStatus, req.getPayChannel).getDesc)
      resp.setOrderStatusCode(PayEnum.PayTradeStatusEnum.getByThirdCode(queryResponse.getTradeStatus.toUpperCase, req.getPayChannel).getCode)
      resp.setOrderStatusMessage(PayEnum.PayTradeStatusEnum.getByThirdCode(queryResponse.getTradeStatus.toUpperCase, req.getPayChannel).getDesc)
      resp.setRespBody(JSON.toJSONString(queryResponse, SerializerFeature.PrettyFormat))
    }
    resp
  }

  @throws[Exception]
  override def refundQuery[T <: QueryOrderReqDTO, R <: QueryOrderRespDTO](req: T, resp: R): R = {
    val refundQueryRequest = new AlipayTradeFastpayRefundQueryRequest
    val refundQueryModel = new AlipayTradeFastpayRefundQueryModel
    refundQueryModel.setOutTradeNo(req.getOriOrderNo)
    refundQueryModel.setOutRequestNo(req.getPayOrderNo)
    refundQueryRequest.setBizModel(refundQueryModel)
    log.debug("alipay refund query request params : {}", JSON.toJSONString(refundQueryRequest, SerializerFeature.PrettyFormat))
    val refundQueryResponse = alipayClient.execute(refundQueryRequest)
    log.debug("alipay refund query response result : {}", JSON.toJSONString(refundQueryResponse, SerializerFeature.PrettyFormat))
    if (!refundQueryResponse.isSuccess) {
      resp.setOrderStatusCode(PayEnum.PayTradeStatusEnum.UN_KNOW.getCode)
      resp.setOrderStatusMessage(PayEnum.PayTradeStatusEnum.UN_KNOW.getDesc)
      resp.setThirdOrderStatusCode(ThirdSystemErrorEnum.getErrorEnum(refundQueryResponse.getSubCode).getCode)
      resp.setThirdOrderStatusMessage(ThirdSystemErrorEnum.getErrorEnum(refundQueryResponse.getSubCode).getMessage)
    }
    else {
      resp.setThirdOrderNo(refundQueryResponse.getTradeNo)
      resp.setThirdOrderStatusCode(PayEnum.RefundTradeStatusEnum.getByThirdCode(refundQueryResponse.getMsg, req.getPayChannel).getThirdCode)
      resp.setThirdOrderStatusMessage(PayEnum.RefundTradeStatusEnum.getByThirdCode(refundQueryResponse.getMsg, req.getPayChannel).getDesc)
      resp.setOrderStatusCode(PayEnum.RefundTradeStatusEnum.getByThirdCode(refundQueryResponse.getMsg, req.getPayChannel).getCode)
      resp.setOrderStatusMessage(PayEnum.RefundTradeStatusEnum.getByThirdCode(refundQueryResponse.getMsg, req.getPayChannel).getDesc)
      resp.setRespBody(JSON.toJSONString(refundQueryResponse, SerializerFeature.PrettyFormat))
    }
    resp
  }

  override def afterPropertiesSet(): Unit = ActionFactory.setQueryAction(getPayChannel, this)
}
